﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLCHBANGIAY.DATA
{
    public class CategoryControler
    {
        Database db = DatabaseFactory.CreateDatabase();
        #region[Category_GetById]
        public List<Category> Category_GetById(string Id)
        {
            List<Category> list = new List<Category>();
            Category obj = new Category();
            DbCommand cmd = db.GetStoredProcCommand("sp_Category_GetById", Id);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.CategoryIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Category_GetByTop]
        public List<Category> Category_GetByTop(string Top, string Where, string Order)
        {
            List<Category> list = new List<Category>();
            Category obj = new Category();
            DbCommand cmd = db.GetStoredProcCommand("sp_Category_GetByTop", Top, Where, Order);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.CategoryIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Category_GetByAll]
        public List<Category> Category_GetByAll(string Lang)
        {
            List<Category> list = new List<Category>();
            Category obj = new Category();
            DbCommand cmd = db.GetStoredProcCommand("sp_Category_GetByAll", Lang);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.CategoryIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Category_GetByLevel]
        public List<Category> Category_GetByLevel(string Level, string Lang, int LevelLength)
        {
            List<Category> list = new List<Category>();
            Category obj = new Category();
            DbCommand cmd = db.GetStoredProcCommand("sp_Category_GetByLevel", Level, Lang, LevelLength);
            using (IDataReader dr = db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    list.Add(obj.CategoryIDataReader(dr));
                }
                dr.Close();
                dr.Dispose();
            }
            return list;
        }
        #endregion
        #region[Category_Paging]
        public List<Category> Category_Paging(string CurentPage, string PageSize, string Lang)
        {
            List<Category> list = new List<Category>();
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Category_Paging", CurentPage, PageSize, Lang))
            {
                try
                {
                    Category obj = new Category();
                    using (IDataReader dr = db.ExecuteReader(cmd))
                    {
                        while (dr.Read())
                        {
                            list.Add(obj.CategoryIDataReader(dr));
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
            return list;
        }
        #endregion
        #region[Category_Insert]
        public bool Category_Insert(Category data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Category_Insert"))
            {
                cmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                cmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                cmd.Parameters.Add(new SqlParameter("@Content", data.Content));
                cmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                cmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
                cmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                cmd.Parameters.Add(new SqlParameter("@Image", data.Image));
                cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                cmd.Parameters.Add(new SqlParameter("@Image2", data.Image2));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
        #region[Category_Update]
        public bool Category_Update(Category data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Category_Update"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                cmd.Parameters.Add(new SqlParameter("@Tag", data.Tag));
                cmd.Parameters.Add(new SqlParameter("@Name", data.Name));
                cmd.Parameters.Add(new SqlParameter("@Content", data.Content));
                cmd.Parameters.Add(new SqlParameter("@Level", data.Level));
                cmd.Parameters.Add(new SqlParameter("@Priority", data.Priority));
                cmd.Parameters.Add(new SqlParameter("@Index", data.Index));
                cmd.Parameters.Add(new SqlParameter("@Image", data.Image));
                cmd.Parameters.Add(new SqlParameter("@Title", data.Title));
                cmd.Parameters.Add(new SqlParameter("@Description", data.Description));
                cmd.Parameters.Add(new SqlParameter("@Keyword", data.Keyword));
                cmd.Parameters.Add(new SqlParameter("@Active", data.Active));
                cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                cmd.Parameters.Add(new SqlParameter("@Lang", data.Lang));
                cmd.Parameters.Add(new SqlParameter("@Image2", data.Image2));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
            //Clear cache
          //  System.Web.HttpContext.Current.Cache.Remove("Category");
            return true;
        }
        #endregion
        #region[Category_Delete]
        public bool Category_Delete(string Id)
        {

            DbCommand cmd = db.GetStoredProcCommand("sp_Category_Delete", Id);
            try
            {
                db.ExecuteNonQuery(cmd);
                return true;
            }
            catch (Exception ex)
            {
                return false;
                //throw ex;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
            }
        }
        #endregion
        #region[Category_Update_Ord]
        public bool Category_Update_Ord(Category data)
        {
            using (DbCommand cmd = db.GetStoredProcCommand("sp_Category_Update_Ord"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Id", data.Id));
                cmd.Parameters.Add(new SqlParameter("@Ord", data.Ord));
                try
                {
                    db.ExecuteNonQuery(cmd);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                    //throw ex;
                }
                finally
                {
                    if (cmd != null) cmd.Dispose();
                }
            }
        }
        #endregion
    }
}
